package com.yonyou.pmclouds.procedure.processor;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.procedure.entity.ProcedureVO;
import com.yonyou.pmclouds.procedure.rmiimpl.ProcedureMaintainImpl;

import static com.yonyou.pmclouds.procedure.rmiimpl.ProcedureMaintainImpl.*;

public class ProcedureInsertRefCheck implements SingleProcessor<ProcedureVO> {

    private ProcedureMaintainImpl maintain;

    public ProcedureInsertRefCheck(ProcedureMaintainImpl maintain) {
        this.maintain = maintain;
    }

    @Override
    public void process(ProcedureVO vo, ProcedureVO orig) throws BusinessException {
        if (vo.getPkParent() == null) {
            return;
        }
        // 引用check
        int status = maintain.getProcedureRefStatus(vo.getPkParent(), vo.getPkTenant());
        if (status == STATUS_NOT_REF) {
            return;
        }
        if (status == STATUS_REF_BY_CHILD) {
            return;
        }
        if(status== STATUS_REF_BY_QUALITY){
            throw new BusinessException("上级工序已经被质量标准引用，不可新增下级！");
        }
        if (status == STATUS_REF_BY_BILL) {
            throw new BusinessException("上级工序已经被引用，不可新增下级！");
        }

    }
}
